home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / spprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.2 KB  |  192 lines

  1. /* Copyright (C) 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: spprint.c,v 1.2 2000/09/19 19:00:51 lpd Exp $ */
  20. /* Print values in ASCII form on a stream */
  21. #include "math_.h"        /* for fabs */
  22. #include "stdio_.h"        /* for stream.h */
  23. #include "string_.h"        /* for strchr */
  24. #include "stream.h"
  25. #include "spprint.h"
  26.  
  27. /* ------ Output ------ */
  28.  
  29. /* Put a byte array on a stream. */
  30. int
  31. pwrite(stream * s, const void *ptr, uint count)
  32. {
  33.     uint used;
  34.  
  35.     sputs(s, (const byte *)ptr, count, &used);
  36.     return (int)used;
  37. }
  38.  
  39. /* Put a string on a stream. */
  40. int
  41. pputs(stream * s, const char *str)
  42. {
  43.     uint len = strlen(str);
  44.     uint used;
  45.     int status = sputs(s, (const byte *)str, len, &used);
  46.  
  47.     return (status >= 0 && used == len ? 0 : EOF);
  48. }
  49.  
  50. /* Print a format string up to the first variable substitution. */
  51. /* Return a pointer to the %, or to the terminating 0 if no % found. */
  52. private const char *
  53. pprintf_scan(stream * s, const char *format)
  54. {
  55.     const char *fp = format;
  56.  
  57.     for (; *fp != 0; ++fp) {
  58.     if (*fp == '%') {
  59.         if (fp[1] != '%')
  60.         break;
  61.         ++fp;
  62.     }
  63.     sputc(s, *fp);
  64.     }
  65.     return fp;
  66. }
  67.  
  68. /* Print (an) int value(s) using a format. */
  69. const char *
  70. pprintd1(stream * s, const char *format, int v)
  71. {
  72.     const char *fp = pprintf_scan(s, format);
  73.     char str[25];
  74.  
  75. #ifdef DEBUG
  76.     if (*fp == 0 || fp[1] != 'd')    /* shouldn't happen! */
  77.     lprintf1("Bad format in pprintd1: %s\n", format);
  78. #endif
  79.     sprintf(str, "%d", v);
  80.     pputs(s, str);
  81.     return pprintf_scan(s, fp + 2);
  82. }
  83. const char *
  84. pprintd2(stream * s, const char *format, int v1, int v2)
  85. {
  86.     return pprintd1(s, pprintd1(s, format, v1), v2);
  87. }
  88. const char *
  89. pprintd3(stream * s, const char *format, int v1, int v2, int v3)
  90. {
  91.     return pprintd2(s, pprintd1(s, format, v1), v2, v3);
  92. }
  93. const char *
  94. pprintd4(stream * s, const char *format, int v1, int v2, int v3, int v4)
  95. {
  96.     return pprintd2(s, pprintd2(s, format, v1, v2), v3, v4);
  97. }
  98.  
  99. /* Print (a) floating point number(s) using a format. */
  100. /* See gdevpdfx.h for why this is needed. */
  101. const char *
  102. pprintg1(stream * s, const char *format, floatp v)
  103. {
  104.     const char *fp = pprintf_scan(s, format);
  105.     char str[50];
  106.  
  107. #ifdef DEBUG
  108.     if (*fp == 0 || fp[1] != 'g')    /* shouldn't happen! */
  109.     lprintf1("Bad format in pprintg: %s\n", format);
  110. #endif
  111.     sprintf(str, "%g", v);
  112.     if (strchr(str, 'e')) {
  113.     /* Bad news.  Try again using f-format. */
  114.     sprintf(str, (fabs(v) > 1 ? "%1.1f" : "%1.8f"), v);
  115.     }
  116.     pputs(s, str);
  117.     return pprintf_scan(s, fp + 2);
  118. }
  119. const char *
  120. pprintg2(stream * s, const char *format, floatp v1, floatp v2)
  121. {
  122.     return pprintg1(s, pprintg1(s, format, v1), v2);
  123. }
  124. const char *
  125. pprintg3(stream * s, const char *format, floatp v1, floatp v2, floatp v3)
  126. {
  127.     return pprintg2(s, pprintg1(s, format, v1), v2, v3);
  128. }
  129. const char *
  130. pprintg4(stream * s, const char *format, floatp v1, floatp v2, floatp v3,
  131.      floatp v4)
  132. {
  133.     return pprintg2(s, pprintg2(s, format, v1, v2), v3, v4);
  134. }
  135. const char *
  136. pprintg6(stream * s, const char *format, floatp v1, floatp v2, floatp v3,
  137.      floatp v4, floatp v5, floatp v6)
  138. {
  139.     return pprintg3(s, pprintg3(s, format, v1, v2, v3), v4, v5, v6);
  140. }
  141.  
  142. /* Print a long value using a format. */
  143. const char *
  144. pprintld1(stream * s, const char *format, long v)
  145. {
  146.     const char *fp = pprintf_scan(s, format);
  147.     char str[25];
  148.  
  149. #ifdef DEBUG
  150.     if (*fp == 0 || fp[1] != 'l' || fp[2] != 'd')    /* shouldn't happen! */
  151.     lprintf1("Bad format in pprintld: %s\n", format);
  152. #endif
  153.     sprintf(str, "%ld", v);
  154.     pputs(s, str);
  155.     return pprintf_scan(s, fp + 3);
  156. }
  157. const char *
  158. pprintld2(stream * s, const char *format, long v1, long v2)
  159. {
  160.     return pprintld1(s, pprintld1(s, format, v1), v2);
  161. }
  162. const char *
  163. pprintld3(stream * s, const char *format, long v1, long v2, long v3)
  164. {
  165.     return pprintld2(s, pprintld1(s, format, v1), v2, v3);
  166. }
  167.  
  168. /* Print (a) string(s) using a format. */
  169. const char *
  170. pprints1(stream * s, const char *format, const char *str)
  171. {
  172.     const char *fp = pprintf_scan(s, format);
  173.  
  174. #ifdef DEBUG
  175.     if (*fp == 0 || fp[1] != 's')    /* shouldn't happen! */
  176.     lprintf1("Bad format in pprints: %s\n", format);
  177. #endif
  178.     pputs(s, str);
  179.     return pprintf_scan(s, fp + 2);
  180. }
  181. const char *
  182. pprints2(stream * s, const char *format, const char *str1, const char *str2)
  183. {
  184.     return pprints1(s, pprints1(s, format, str1), str2);
  185. }
  186. const char *
  187. pprints3(stream * s, const char *format, const char *str1, const char *str2,
  188.      const char *str3)
  189. {
  190.     return pprints2(s, pprints1(s, format, str1), str2, str3);
  191. }
  192.